﻿
namespace Northwind.ViewModel
{
    using GalaSoft.MvvmLight;
    using System.Windows;
    using System.Activities;
    using Northwind.ViewModel.Workflows;
    using Northwind.ViewModel.Enumerations;
    using System.ComponentModel;
    using GalaSoft.MvvmLight.Messaging;
    using Northwind.ViewModel.Messeges;

    using System.Windows.Threading;
    using System.Collections.Generic;

    public class OrderWizardViewModel : ViewModelBase
    {
        #region Public Properties

        bool _isCloseView;
        public bool IsCloseView
        {
            get { return _isCloseView; }
            private set
            {
                _isCloseView = value;
                RaisePropertyChanged("IsCloseView");
            }
        }

        #endregion

        #region Private Fields

        WorkflowApplication ViewStateController;
        
        #endregion

        #region Order Related Properties

        OrderWizardCustomerViewModel _customerSearchBoxViewModel;
        public OrderWizardCustomerViewModel CustomerDetails 
        {
            get
            {
                if (_customerSearchBoxViewModel == null)
                {
                    _customerSearchBoxViewModel = new OrderWizardCustomerViewModel();
                }

                return _customerSearchBoxViewModel;
            }
        }

        OrderWizardEmployeesViewModel _employeesViewModel;
        public OrderWizardEmployeesViewModel Employees
        {
            get
            {
                if (_employeesViewModel == null)
                {
                    _employeesViewModel = new OrderWizardEmployeesViewModel();
                }

                return _employeesViewModel;
            }
        }

        OrderWizardOrderDetailsViewModel _orderDetails;
        public OrderWizardOrderDetailsViewModel OrderDetails
        {
            get
            {
                if (_orderDetails == null)
                {
                    _orderDetails = new OrderWizardOrderDetailsViewModel();
                }
                
                return _orderDetails;
            }
        }
        #endregion

        #region Wizard Child Properties

        Visibility _customerViewVisibility = Visibility.Collapsed;
        public Visibility CustomerViewVisibility
        {
            get { return _customerViewVisibility; }
            set
            {
                _customerViewVisibility = value;
                RaisePropertyChanged("CustomerViewVisibility");
            }
        }

        Visibility _employeeViewVisibility = Visibility.Collapsed;
        public Visibility EmployeeViewVisibility
        {
            get { return _employeeViewVisibility; }
            set
            {
                _employeeViewVisibility = value;
                RaisePropertyChanged("EmployeeViewVisibility");
            }
        }

        Visibility _orderDetailsViewVisibility = Visibility.Collapsed;
        public Visibility OrderDetailsViewVisibility
        {
            get { return _orderDetailsViewVisibility; }
            set
            {
                _orderDetailsViewVisibility = value;
                RaisePropertyChanged("OrderDetailsViewVisibility");
            }
        }

        #endregion

        #region Constructors

        public OrderWizardViewModel()
        {
            Initialize();
            
        }

        private void Initialize()
        {
            SubscribeEventHandlers();

            //Initialize Workflow 
            Dictionary<string, object> arguments = new Dictionary<string, object>() { { "orderWizardViewModel", this } };
            ViewStateController = new WorkflowApplication(new OrderWizardControlFlow(), arguments);
            ViewStateController.SynchronizationContext = DispatcherSynchronizationContext.Current;

            ViewStateController.Run();
        }

        private void SubscribeEventHandlers()
        {
            this.CustomerDetails.PropertyChanged += new PropertyChangedEventHandler(CustomerDetails_PropertyChanged);
            this.Employees.PropertyChanged += new PropertyChangedEventHandler(Employees_PropertyChanged);
            this.OrderDetails.PropertyChanged += new PropertyChangedEventHandler(OrderDetails_PropertyChanged);
        }

        void OrderDetails_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ShowSibling")
            {
                if (OrderDetails.ShowSibling == WizardChildFlow.ShowNextChild)
                {
                    ViewStateController.ResumeBookmark("FinishOrderWizard", null);
                }
                else if (OrderDetails.ShowSibling == WizardChildFlow.ShowPreviousChild)
                {
                    ViewStateController.ResumeBookmark("EnterEmployeeInformation", null);
                }
            }
        }

        void Employees_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ShowSibling")
            {
                if (Employees.ShowSibling == WizardChildFlow.ShowNextChild)
                {
                    ViewStateController.ResumeBookmark("EnterOrderDetailsInformation", null);
                }
                else if (Employees.ShowSibling == WizardChildFlow.ShowPreviousChild)
                {
                    ViewStateController.ResumeBookmark("EnterCustomerInformation", null);
                }
            }
        }

        void CustomerDetails_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.PropertyName == "ShowSibling")
            {
                if (CustomerDetails.ShowSibling == WizardChildFlow.ShowNextChild)
                {
                    ViewStateController.ResumeBookmark("EnterEmployeeInformation", null);
                }                
            }
        }

        #endregion

        #region State Transition Methods

        public void ShowCustomerView()
        {
            CustomerViewVisibility = Visibility.Visible;
        }

        public void CloseCustomerView()
        {
            CustomerViewVisibility = Visibility.Collapsed;
        }

        public void ShowEmployeeView()
        {
            EmployeeViewVisibility = Visibility.Visible;
        }

        public void CloseEmployeeView()
        {
            EmployeeViewVisibility = Visibility.Collapsed;
        }

        public void ShowOrderDetailsView()
        {
            this.OrderDetails.Customer = CustomerDetails.SelectedCustomer;
            this.OrderDetails.Employee = Employees.SelectedEmployee;

            OrderDetailsViewVisibility = Visibility.Visible;
        }

        public void CloseOrderDetailsView()
        {
            OrderDetailsViewVisibility = Visibility.Collapsed;
        }

        public void FinishWizard()
        {
            IsCloseView = true;
            Messenger.Default.Send<NewOrderAdded>(new NewOrderAdded() { Order = this.OrderDetails });
            Messenger.Default.Send<OrderWizardClosed>(new OrderWizardClosed());
        }
        #endregion

    }
}
